package com.fg.cloud.manager.service.factory.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.encrypt.Encrypt;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.factory.FactoryUserEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.system.vo.RoleVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.factory.FactoryUserMapper;
import com.fg.cloud.manager.mapper.system.RoleMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.FactoryUserService;

/**
 * Description:厂商用户service实现
 * 
 * @author shaoqiping
 * @date 2017年8月4日下午5:32:56
 */
@Service
public class FactoryUserServiceImpl extends BaseServiceImpl implements FactoryUserService {

	@Autowired
	private FactoryUserMapper factoryUserMapper;

	@Autowired
	private RoleMapper user_roleMapper;

	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return factoryUserMapper;
	}
	
	/**
	 * 厂商用户角色授权
	 */
	@Override
	@Transactional
	public boolean roleAutho(Long userId, Long roleId) {
		return factoryUserMapper.roleAutho(userId, roleId) > 0 ? true : false;
	}

	@Override
	public FactoryUserVo selectUserByUserName(String userName) {
		FactoryUserVo user = factoryUserMapper.selectUserByUserName(userName);
		return user;
	}

	@Override
	public FactoryUserVo selectUserByLogin(String userName, String passWord) {
		return factoryUserMapper.selectUserByLogin(userName, passWord);
	}

	@Override
	@Transactional
	public boolean updateByPrimaryKeySelective(FactoryUserEntity factoryUser) {
		//处理修改密码
		factoryUser.setPassword(Encrypt.md5(factoryUser.getPassword()));
		return factoryUserMapper.updateByPrimaryKeySelective(factoryUser) > 0 ? true : false;
	}

	@Override
	@Transactional
	public boolean insertSelective(FactoryUserEntity factoryUser) {
		factoryUser.setPassword(Encrypt.md5("123456"));
		factoryUser.setUserType(1);
		factoryUser.setStatus(1);
		return factoryUserMapper.insertSelective(factoryUser) > 0 ? true : false;
	}

	@Override
	@Transactional
	public boolean deletes(Map<String, Object> map) throws IOException {
		String[] idArray = ((String) map.get("ids")).split("[, ]+");
		map.put("ids", idArray);
		List<FactoryUserEntity> userList = factoryUserMapper.selectFactoryByIds(idArray);
		// 判断删除数据是否成功
		int count = 0;
		count += factoryUserMapper.deletes(map);
		count += factoryUserMapper.deleteUserRole(userList);
		return count > 0 ? true : false;
	}


	@Override
	public void findList(PageUtils<FactoryUserVo> pageinfo) {
		long total = factoryUserMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(factoryUserMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<FactoryUserVo>());
		}
	}
	
	@Override
	public void selectPage(PageUtils<FactoryUserVo> pageinfo) {
		long total = factoryUserMapper.filterCounts(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if ( total > 0 ) {
			pageinfo.setData(factoryUserMapper.findLists(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<FactoryUserVo>());
		}
	}

	@Override
	public List<RoleVo> findByFacId(Long userId, Long facId) {
		List<RoleVo> roleList = user_roleMapper.selectUserRoles(facId);
		List<Long> roleIds = user_roleMapper.selectByUserId(userId);
		for (RoleVo role : roleList) {
			if (roleIds.contains(role.getId()))
				role.setChecked(true);
		}
		return roleList;
	}

	@Override
	@Transactional
	public boolean updateUserRoles(Long userId, String roleIds) {
		List<Long> list = user_roleMapper.selectByUserId(userId);
		Long[] deletes = changeElements(LongUtils.convert(roleIds.split(",")), list.toArray(new Long[list.size()]));
		Long[] adds = changeElements(list.toArray(new Long[list.size()]),LongUtils.convert(roleIds.split(",")));
		int count = 0;
		if(deletes.length!=0){
			count = factoryUserMapper.deleteSpecifiedUserRole(userId, deletes);
		}
		if(adds.length!=0){
			count += factoryUserMapper.batchInsert(userId, adds);
		}
		return count >= 0 ? true : false;
	}

	/**
	 * Description:新旧关联比较--新在前返回删除  旧在前返回新增
	 * 
	 * @param news
	 * @param olds
	 * @return Long[]
	 * @author shaoqiping
	 * @date 2017年8月11日上午8:52:31
	 */
	public Long[] changeElements(Long[] news, Long[] olds) {
		List<Long> list= new ArrayList<Long>();
		for (Long i : olds) {
			int temp=0;
			for (Long j : news) {
				if(i==j){
					break;
				}
				temp++;
			}
			if(temp==news.length){
				list.add(i);
			}
		}
		return list.toArray(new Long[list.size()]);
	}

	@Override
	public FactoryUserEntity findToCheck(Long id) {
		return factoryUserMapper.selectToCheck(id);
	}

	@Override
	public FactoryUserVo selectUserByUserName(String userName, Long facId) {
		return factoryUserMapper.selectUserByUserName2(userName, facId);
	}

	@Override
	public FactoryUserVo selectUserByUserName(String userName, Long facId, Long id) {
		return factoryUserMapper.selectUserByUserName3(userName, facId, id);
	}

	@Override
	public boolean removes(Long[] ids) {
		return factoryUserMapper.removes(ids) > 0 ? true : false;
	}
	
	@Override
	public boolean resetPsd(Long[] ids) {
		return factoryUserMapper.resetPsd(ids, Encrypt.md5("123456")) > 0 ? true : false;
	}
	
}
